<?php
if(!defined('IN_ORA')) {
	exit('Access Denied');
}
session_start();
$GLOBALS['__page_cache__'] = true;
$act = $GLOBALS['act'] ==""?"index":$GLOBALS['act'];
$GLOBALS['tpl']->assign('act',$act);

/*** 查看新闻 ***/
if($act == "v"){
	$id = intval($GLOBALS['_param']['id']);
	if(!$GLOBALS['tpl']->isCached("art-v-id_$id")){
		/** 站点信息 **/
		$GLOBALS['tpl']->assign('sinfo',$GLOBALS['db']->getOne('select * from '.tn('info').' order by Id asc limit 1'));

		/** 导航信息 **/
		$GLOBALS['tpl']->assign('navs',$GLOBALS['db']->getAll('select * from '.tn('nav').' order by nav_sort asc limit 6'));

		/** 新闻分类 **/
		import("lib.cat");
		$GLOBALS['tpl']->assign('cates',cat_lib::getSubChild(0,2,' and cat_type=0'));

		$sql = "select oa.*,oc.cat_name,oc.path,octont.content from ".tn('article')." oa ";
		$sql.= "left join ".tn('cat')." oc on oa.cat_id = oc.Id ";
		$sql.= "left join ".tn('content')." octont on octont.fk_id = oa.Id ";
		$sql.= " where octont.ctype = 0 and oa.id= $id ";
		$art = $GLOBALS['db']->getOne($sql);
		if(!$art){
			redirect("index-index|true");
		}
		if($art['tags']){
			$art['tags'] = explode(",",$art['tags']);
		}
		$GLOBALS['tpl']->assign('art',$art);

		/** 当前位置 **/
		$local = array();
		$local[] = array('url'=>parse_uri('art-index'),'text'=>'新闻动态');
		$q = $GLOBALS['db']->query("select Id,cat_name from ".tn('cat')." where id in(".str_replace('-',',',$art['path']).")");
		while ($row=$GLOBALS['db']->fetch_array($q)) {
			$local[] = array('url'=>parse_uri('art-cat-id-%d',$row['Id']),'text'=>$row['cat_name']);
		}
		$local[] = array('url'=>parse_uri('art-cat-id-%d',$art['cat_id']),'text'=>$art['cat_name']);
		$local[] = array('url'=>parse_uri('art-v-id-%d',$art['Id']),'text'=>$art['title']);
		$GLOBALS['tpl']->assign('local',$local);
		unset($art);
	}
	/** 浏览计数 **/
	if(!isset($_COOKIE["views"]["art-$id"])){
		$GLOBALS['db']->execute("update ".tn('article')." set views = views + 1");
		setcookie("views[art-$id]", "ora",time()+3600);
	}
	$GLOBALS['tpl']->assign('views',$GLOBALS['db']->count("select views from ".tn('article')." where Id = $id"));

	$GLOBALS['tpl']->display("article.html");
}

/** 新闻动态首页 **/
else if($act == 'index'){
	if(!$GLOBALS['tpl']->isCached("art-index")){
		/** 站点信息 **/
		$GLOBALS['tpl']->assign('sinfo',$GLOBALS['db']->getOne('select * from '.tn('info').' order by Id asc limit 1'));

		/** 导航信息 **/
		$GLOBALS['tpl']->assign('navs',$GLOBALS['db']->getAll('select * from '.tn('nav').' order by nav_sort asc limit 6'));

		/** 新闻分类 **/
		import("lib.cat");
		$cates = cat_lib::getSubChild(0,2,' and cat_type=0');
		$GLOBALS['tpl']->assign('cates',$cates);

		/*分类下属内容*/
		$catArts = array();
		for ($j=0;$j<sizeof($cates);$j++){
			$tmp = $cates[$j];
			unset($tmp['sub']);
			$sql = "select oa.Id,oa.title,oa.keywords,oa.cat_id,oa.dateline,oa.pic,oc.cat_name ";
			$sql.= "from ".tn('article')." oa left join ".tn('cat')." oc on oa.cat_id = oc.Id ";
			$sql.= " where oa.status = 1 and oa.cat_id in (".cat_lib::getCatIdStr($tmp['Id']).") order by oa.Id desc  limit 8 ";
			$tmp['arts'] = array();
			$now = strtotime(date("Y-m-d",time()));
			$query = $GLOBALS['db']->query($sql);
			while ($row = $GLOBALS['db']->fetch_array($query)) {
				if( $now - strtotime(date("Y-m-d",$row['dateline'])) <= 86400 ){
					$row['isnew'] = true;
				}
				$tmp['arts'][] = $row;
			}
			$catArts[] = $tmp;
		}
		$GLOBALS['tpl']->assign('catArts',$catArts);
		unset($cates);
		unset($catArts);
		/** 专题标签 **/
		$GLOBALS['tpl']->assign('tags',$GLOBALS['db']->getAll('select * from '.tn('tags').' where tag_type=0 group by tag_name order by Id desc'));
		/** 当前位置 **/
		$local = array();
		$local[] = array('url'=>parse_uri('art-index'),'text'=>'新闻动态');
		$GLOBALS['tpl']->assign('local',$local);
	}
	$GLOBALS['tpl']->display("article_index.html");
}
/** 动态分类 **/
else if($act == 'cat'){
	$id = intval($GLOBALS['_param']['id']);
	$pid= intval($GLOBALS['_param']['p']);
	$pid= $pid>0?$pid:1;
	if($id<=0){
		redirect(HTTP_HOST,false);
	}
	import("lib.page");
	if(!$GLOBALS['tpl']->isCached("art-cat-id-$id-pid-$pid")){
		/** 站点信息 **/
		$GLOBALS['tpl']->assign('sinfo',$GLOBALS['db']->getOne('select * from '.tn('info').' order by Id asc limit 1'));

		/** 导航信息 **/
		$GLOBALS['tpl']->assign('navs',$GLOBALS['db']->getAll('select * from '.tn('nav').' order by nav_sort asc limit 6'));

		/** 新闻分类 **/
		import("lib.cat");
		$cates = cat_lib::getSubChild(0,2,' and cat_type=0');
		$GLOBALS['tpl']->assign('cates',$cates);
		$GLOBALS['tpl']->assign('curr_cat_id',$id);
		$curr_cat = $GLOBALS['db']->getOne("select * from ".tn('cat')." where id = $id");
		if(!$curr_cat){
			redirect(HTTP_HOST,false);
		}
		$GLOBALS['tpl']->assign('curr_cat',$curr_cat);
		/** 专题标签 **/
		$GLOBALS['tpl']->assign('tags',$GLOBALS['db']->getAll('select * from '.tn('tags').' where tag_type=0 group by tag_name order by Id desc'));
		/** 当前位置 **/
		$local = array();
		$local[] = array('url'=>parse_uri('art-index'),'text'=>'新闻动态');
		$q = $GLOBALS['db']->query("select Id,cat_name from ".tn('cat')." where id in(".str_replace('-',',',$curr_cat['path']).")");
		while (@$row=$GLOBALS['db']->fetch_array($q)) {
			$local[] = array('url'=>parse_uri('art-cat-id-%d',$row['Id']),'text'=>$row['cat_name']);
		}
		$local[] = array('url'=>parse_uri('art-cat-id-%d',$curr_cat['Id']),'text'=>$curr_cat['cat_name']);
		$GLOBALS['tpl']->assign('local',$local);

		$psize = $GLOBALS['_param']['psize']?$GLOBALS['_param']['psize']:$GLOBALS['ora_cfg']['page_size'];
		$idStr = cat_lib::getCatIdStr($id);
		$total = $GLOBALS['db']->count("select count(*) from ".tn('article')." where status = 1 and cat_id in ($idStr) ");

		$pobj = new page('p',$pid,$total,$psize);
		$sql = "select oa.*,oc.cat_name from  ".tn("article")." oa inner join ( select Id from  ".tn("article");
		$sql .= " where cat_id in (".$idStr.") order by Id desc limit ".$pobj->sqlParam.") b using(Id) left join ".tn("cat")." oc on oa.cat_id = oc.Id";
		if($pid >= intval($pobj->maxPage/2)){
			$sql = "select oa.*,oc.cat_name from  ".tn("article")." oa inner join ( select Id from  ".tn("article");
			$sql .= " where cat_id in (".$idStr.") order by Id asc limit ".$pobj->sqlParam.") b using(Id) left join ".tn("cat")." oc on oa.cat_id = oc.Id";
		}
		$GLOBALS['tpl']->assign("pobj",$pobj);
		$url = get_urlStr();
		$GLOBALS['tpl']->assign('url',preg_replace('/\-?p(\-|_)\d+/i',"",$url));
		$GLOBALS['tpl']->assign('arts',$GLOBALS['db']->getAll($sql));

	}
	$GLOBALS['tpl']->display("article_cat.html");
}
?>